package com.gf.framework.translatordemo.controller;

import com.gf.framework.common.utils.ObjectMapper;
import com.gf.framework.translatordemo.dto.StudentDTO;
import com.gf.framework.translatordemo.vo.StudentVo;
import com.gf.framework.translatordemo.vo.TeacherVo;
import com.gf.framework.web.annotation.Translator;
import com.gf.framework.web.translate.TranslatorUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;

/**
 * @author gaofei
 * @Description
 * @create 2022/5/26 11:36
 */
@RestController
@Api(tags = "测试")
@Slf4j
public class TestController {

    @Autowired
    TranslatorUtil translatorUtil;

    /**
     * 转义demo
     * @return
     */
    @Translator
    @GetMapping("getstudent")
    public StudentVo getStudent() {
        StudentVo studentVo = ObjectMapper.map(new StudentDTO(10, "gaofei",
                LocalDate.of(2010, 1, 1), "17302899067"), StudentVo.class);
        log.info(String.format("getstudent:%s", studentVo));

        return studentVo;
    }

    @GetMapping("getstudent2")
    public StudentVo getstudent2() {
        StudentVo studentVo = ObjectMapper.map(new StudentDTO(10, "gaofei",
                LocalDate.of(2010, 1, 1), "17302899067"), StudentVo.class);
        log.info(String.format("翻译前:%s", studentVo));
        studentVo = translatorUtil.translator(studentVo);
        log.info(String.format("翻译后:%s", studentVo));
        return studentVo;
    }

    /**
     * 根据入参条件，判断是不是需要对返回值进行翻译
     * @param detail
     * @return
     */
    @Translator(condition = "#detail")
    @GetMapping("getteacher")
    public TeacherVo getTeacher(
            @RequestParam(name = "detail", required=false, defaultValue = "false")
                    Boolean detail) {
        TeacherVo teacherVo = new TeacherVo();
        teacherVo.setTeacherId(1);
        teacherVo.setName("gaofei");
        return teacherVo;
    }
}
